package com.tang.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.constants.SystemConstants;
import com.tang.dto.LinkDto;
import com.tang.enums.AppHttpCodeEnum;
import com.tang.sys.ResponseResult;
import com.tang.sys.entity.Link;
import com.tang.sys.mapper.LinkMapper;
import com.tang.sys.service.ILinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.sys.vo.LinkVo;
import com.tang.sys.vo.PageVo;
import com.tang.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 友链 服务实现类
 * </p>
 *
 * @author shankaka
 * @since 2023-04-14
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements ILinkService {

    @Autowired
    private LinkMapper linkMapper;
    @Autowired
    private ILinkService iLinkService;

    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通过的
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_YESRMAL);
        List<Link> links = list(queryWrapper);
        //转换成vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        //封装返回
        return ResponseResult.okResult(linkVos);
    }



    @Override
    public ResponseResult<LinkVo> getAllLinkList(Integer pageNum, Integer pageSize, LinkDto linkDto) {
        //分页查询
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(linkDto.getName()),Link::getName,linkDto.getName());
        wrapper.eq(StringUtils.hasText(linkDto.getStatus()),Link::getStatus,linkDto.getStatus());

        Page<Link> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        page(page,wrapper);
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    //新增友链
    @Override
    public ResponseResult<LinkVo> addLink(LinkVo linkVo) {
        Link link = new Link();
        link.setName(linkVo.getName());
        link.setDescription(linkVo.getDescription());
        link.setAddress(linkVo.getAddress());
        link.setLogo(linkVo.getLogo());
        link.setStatus(linkVo.getStatus());

        linkMapper.insert(link);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLink(Long id) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Link::getId,id);
        this.linkMapper.delete(wrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLinkInfo(Long id) {
        Link byId = iLinkService.getById(id);
        if (byId != null){
            LinkVo linkVo = new LinkVo();
            BeanUtils.copyProperties(byId, linkVo);
            return ResponseResult.okResult(linkVo);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.valueOf("未找到"));
        }


    }

    @Override
    public ResponseResult<LinkVo> updateLink(LinkVo linkVo) {
        Link link = linkMapper.selectById(linkVo.getId());
        if (link == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.valueOf("未找到"));
        }
        link.setName(linkVo.getName());
        link.setAddress(linkVo.getAddress());
        link.setDescription(linkVo.getDescription());
        link.setLogo(linkVo.getLogo());
        link.setStatus(linkVo.getStatus());

        int insert = linkMapper.updateById(link);

        if (insert == 1){
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.valueOf("未找到"));
        }
    }
}
